package com.itheima.service.db.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.PmsProduct;
import com.itheima.mapper.PmsProductMapper;
import com.itheima.service.db.PmsProductService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class PmsProductServiceImpl implements PmsProductService {
    @Autowired
    PmsProductMapper pmsProductMapper;
    @Override
    public Page<PmsProduct> getByIdProduct(Integer brandId, String keyword, Integer pageNum, Integer pageSize,
                                           Integer productCategoryId, String productSn,
                                           Integer publishStatus, Integer verifyStatus) {
        Page<PmsProduct> page=new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<PmsProduct> lqw=new LambdaQueryWrapper<>();
        lqw.eq(brandId!=null,PmsProduct::getBrandId,brandId)
                .like(StrUtil.isNotBlank(keyword),PmsProduct::getKeywords,keyword)
                .eq(productCategoryId!=null,PmsProduct::getProductCategoryId,productCategoryId)
                .like(StrUtil.isNotBlank(productSn),PmsProduct::getProductSn,productSn)
                .eq(publishStatus!=null,PmsProduct::getPublishStatus,publishStatus)
                .eq(verifyStatus!=null,PmsProduct::getVerifyStatus,verifyStatus)
                .eq(PmsProduct::getDeleteStatus,0)
                .orderByDesc(PmsProduct::getSort);
        
        return pmsProductMapper.selectPage(page,lqw);
    }

    @Override
    public Boolean deleteStatus(Integer deleteStatus, Integer[] ids) {
        LambdaUpdateWrapper<PmsProduct> luw=new LambdaUpdateWrapper<>();
        luw.in(ids!=null,PmsProduct::getId,ids)
                .set(PmsProduct::getDeleteStatus,deleteStatus);
        int update = pmsProductMapper.update(null, luw);
        return update>0;
    }

    @Override
    public Boolean newStatus(Integer newStatus, Integer[] ids) {
        LambdaUpdateWrapper<PmsProduct> luw=new LambdaUpdateWrapper<>();
        luw.in(ids!=null,PmsProduct::getId,ids)
                .set(PmsProduct::getNewStatus,newStatus);
        int update = pmsProductMapper.update(null, luw);
        return update>0;
    }

    @Override
    public Boolean publishStatus(Integer publishStatus, Integer[] ids) {
        LambdaUpdateWrapper<PmsProduct> luw=new LambdaUpdateWrapper<>();
        luw.in(ids!=null,PmsProduct::getId,ids)
                .set(PmsProduct::getPublishStatus,publishStatus);
        int update = pmsProductMapper.update(null, luw);
        return update>0;
    }

    @Override
    public Boolean recommendStatus(Integer recommendStatus, Integer[] ids) {
        LambdaUpdateWrapper<PmsProduct> luw=new LambdaUpdateWrapper<>();
        luw.in(ids!=null,PmsProduct::getId,ids)
                .set(PmsProduct::getRecommandStatus,recommendStatus);
        int update = pmsProductMapper.update(null, luw);
        return update>0;
    }

    @Override
    public PmsProduct selectById(Integer id) {
        return pmsProductMapper.selectById(id);
    }

    @Override
    public void updateById(PmsProduct pmsProduct, Integer id) {
        LambdaQueryWrapper<PmsProduct> lqw=new LambdaQueryWrapper<>();
        lqw.eq(PmsProduct::getId,id);
        pmsProductMapper.update(pmsProduct,lqw);
    }

    @Override
    public Long save(PmsProduct pmsProduct) {
        pmsProductMapper.insert(pmsProduct);
        return pmsProduct.getId();
    }
}
